package com.exer1.servlet;

import com.exer1.pojo.Dormitory;
import com.exer1.service.DormitoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName DormitoryServlet
 * @Description 添加宿舍信息
 * @Author Kx
 * @Date 2025/4/23 21:01
 * @Version 1.0
 */
@WebServlet("/InsertDormitoryServlet")
public class InsertDormitoryServlet extends HttpServlet {

    // 实例化业务层对象
    private final DormitoryService dormitoryService = new DormitoryService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求参数
        String stuName = req.getParameter("stuName");
        String stuSex = req.getParameter("stuSex");
        String clsName = req.getParameter("clsName");
        String inDate = req.getParameter("inDate");
        String becNo = req.getParameter("becNo");

        try {
            // 把字符串转换时间类型
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = sdf.parse(inDate);

            // 把字符串转成整数类型
            int bec = Integer.parseInt(becNo);

            // 封装到实体类中
            Dormitory dormitory = new Dormitory();
            dormitory.setStuName(stuName);
            dormitory.setStuSex(stuSex);
            dormitory.setClsName(clsName);
            dormitory.setInDate(parse);
            dormitory.setBecNo(bec);

            // 调用方法
            dormitoryService.insertDormitory(dormitory);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
